package com.openNetty.server.timertask.exector;

import com.openNetty.common.bean.*;
import com.openNetty.common.system.LoggerUtil;
import com.openNetty.server.main.Global;
import com.openNetty.server.proxy.BackSqlFileProxy;

import java.io.IOException;

import static com.openNetty.common.utils.EncryptUtil.getKey;

/**
 * 数据库系统文件恢复下载
 *
 * @author zhenggm
 * @create 2017-12-08 上午 9:37
 **/


public class DownFileExector {
    private static LoggerUtil logger = LoggerUtil.getLogger(DownFileExector.class);
    BackSqlFileProxy backSqlFileProxy = new BackSqlFileProxy();

    public ComResponse task() {
        ComResponse comResponse = null;
        try {
            BackNode backNode = new BackNode();
            backNode.setRoot(Global.properties.getProperty("back.root"));
            backNode.setAddress(Global.properties.getProperty("back.address"));
            backNode.setPort(Global.properties.getProperty("back.port"));
            comResponse = getSqlBackFile(backNode);

        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
        }
        return comResponse;
    }

    /**
     * 从远程服务器下载sql备份文件
     *
     * @param backNode
     */
    private ComResponse getSqlBackFile(BackNode backNode) {
        HttpAck ack = new HttpAck();
        ack.setCount(0);
        ack.setPostition(0);
        ack.setFileName(Global.properties.getProperty("back.filename"));
        ack.setKey(getKey(Global.keyMap));
        ack.setPass(Global.keyMap.get(ack.getKey()));
        // 构建http请求类
        ComRequest comRequest = new ComRequest();
        comRequest.setBackNode(backNode);
        comRequest.setMethod(HttpInfo.GET);
        comRequest.setMessage(Command.GET_SQL_BACKFILE);
        comRequest.setHttpAck(ack);
        ComResponse comResponse = backSqlFileProxy.getSqlBackFile(comRequest);
        if (comResponse.isSuccess()) {
            logger.debug("任务执行完成");
        } else {
            logger.debug("任务执行失败");
        }
        return comResponse;
    }
}
